package com.liang.shiro;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import com.liang.exception.CaptchaEmptyException;
import com.liang.exception.CaptchaErrorException;
import com.liang.pojo.Role;
import com.liang.pojo.User;
import com.liang.service.RoleService;
import com.liang.service.UserService;

public class MyRealm extends AuthorizingRealm{
	@Autowired
	UserService userService;
	@Autowired
	RoleService roleService;

	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		String username = principals.getPrimaryPrincipal().toString();
		
//		  Set<String> roles = rootService.selectRoleByUsername(username); 
//		  Set<String> permissions = rootService.selectPremissionByUsername(username);
//		  SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//		  info.setRoles(roles); 
//		  info.setStringPermissions(permissions); 
//		  return info;
		
		List<Role> list = roleService.selectAllRoleByUsername(username);
		Set<String> set=new TreeSet<String>();
		for (Role role : list) {
			set.add(role.getRole());
		}
		System.out.println(set);
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		info.setRoles(set); 
		return info;
	}

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		 // 在自定义的认证过滤器中将验证码保存至KaptchaCodeToken中
        // 此处的Token就是认证过滤器中实例化的Token,可以直接强制转换
		CaptchaToken captchaToken=(CaptchaToken)token;
		String captchacode = captchaToken.getCaptchacode();
		System.out.println(captchacode);
		if(captchacode==null || "".equals(captchacode)) {
			throw new CaptchaEmptyException();
		}
		// Kaptcha在生成验证码时会将验证码放入SESSION中
        // 默认KEY为KAPTCHA_SESSION_KEY, 可以在Web.xml中配置
		String sessionCaptcha = (String) SecurityUtils.getSubject().getSession().getAttribute("KAPTCHA_SESSION_KEY");
		System.out.println(sessionCaptcha);
		if(!captchacode.equals(sessionCaptcha)) {
			throw new CaptchaErrorException();
		}
		String username = captchaToken.getUsername();
		String username2 = captchaToken.getPrincipal().toString();

		System.out.println("++++++++++++++++++++++++"+username);
		System.out.println("++++++++++++++++++++++++"+username2);
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("username", username);
		User user = userService.selectUserByUsername(map);
		if(user==null) {
			return null;
		}
		System.out.println(user);
		ByteSource salt = ByteSource.Util.bytes(user.getSalt());
		AuthenticationInfo info = new SimpleAuthenticationInfo(username,user.getPassword(),salt,this.getName());
		return info;
	}
}
